// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
namespace Microsoft.Xrm.Sdk.Query
{
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime.Serialization;
    using Microsoft.Xrm.Sdk.Temporary;

	[DataContract(Name = "LinkEntity", Namespace = XmlNamespaces.V5.Contracts)]
	public sealed class LinkEntity : IExtensibleDataObject
	{
		public LinkEntity()
			: this(null, null, null, null, JoinOperator.Inner)
		{
		}

		public LinkEntity(string linkFromEntityName, string linkToEntityName, string linkFromAttributeName, string linkToAttributeName, JoinOperator joinOperator)
		{
			_linkFromEntityName = linkFromEntityName;
			_linkToEntityName = linkToEntityName;
			_linkFromAttributeName = linkFromAttributeName;
			_linkToAttributeName = linkToAttributeName;
			_joinOperator = joinOperator;

			_columns = new ColumnSet();
			_linkCriteria = new FilterExpression();
		}

		[DataMember]
		public string LinkFromAttributeName
		{
			get
			{
				return _linkFromAttributeName;
			}
			set
			{
				_linkFromAttributeName = value;
			}
		}

		[DataMember]
		public string LinkFromEntityName
		{
			get
			{
				return _linkFromEntityName;
			}
			set
			{
				_linkFromEntityName = value;
			}
		}

		[DataMember]
		public string LinkToEntityName
		{
			get
			{
				return _linkToEntityName;
			}
			set
			{
				_linkToEntityName = value;
			}
		}

		[DataMember]
		public string LinkToAttributeName
		{
			get
			{
				return _linkToAttributeName;
			}
			set
			{
				_linkToAttributeName = value;
			}
		}

		[DataMember]
		public JoinOperator JoinOperator
		{
			get
			{
				return _joinOperator;
			}
			set
			{
				_joinOperator = value;
			}
		}

		[DataMember]
		public FilterExpression LinkCriteria
		{
			get
			{
				return _linkCriteria;
			}
			set
			{
				_linkCriteria = value;
			}
		}

		[DataMember]
		public DataCollection<LinkEntity> LinkEntities
		{
			get
			{
				if (_linkEntities == null)
				{
					_linkEntities = new DataCollection<LinkEntity>();
				}
				return _linkEntities;
			}
			[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Called via reflection")]
			private set
			{
				_linkEntities = value;
			}
		}

		[DataMember]
		public ColumnSet Columns
		{
			get
			{
				if (_columns == null)
				{
					_columns = new ColumnSet();
				}
				return _columns;
			}
			set
			{
				_columns = value;
			}
		}

		[DataMember]
		public string EntityAlias
		{
			get
			{
				return _entityAlias;
			}
			set
			{
				_entityAlias = value;
			}
		}


		internal void Accept(IQueryVisitor visitor)
		{
			visitor.Visit(this);
		}

		public LinkEntity AddLink(string linkToEntityName, string linkFromAttributeName, string linkToAttributeName)
		{
			return AddLink(linkToEntityName, linkFromAttributeName, linkToAttributeName, JoinOperator.Inner);
		}

		public LinkEntity AddLink(string linkToEntityName, string linkFromAttributeName, string linkToAttributeName, JoinOperator joinOperator)
		{
			LinkEntity link = new LinkEntity(
				_linkFromEntityName,
				linkToEntityName,
				linkFromAttributeName,
				linkToAttributeName,
				joinOperator);

			this.LinkEntities.Add(link);

			return link;
		}

		private string _linkFromEntityName;
		private string _linkFromAttributeName;
		private string _linkToEntityName;
		private string _linkToAttributeName;
		private JoinOperator _joinOperator;
		private FilterExpression _linkCriteria;
		private string _entityAlias;
		private ColumnSet _columns;
		private DataCollection<LinkEntity> _linkEntities;

		#region IExtensibleDataObject Implementation
		private ExtensionDataObject ExtensionDataObject;
		public ExtensionDataObject ExtensionData
		{
			get
			{
				return ExtensionDataObject;
			}
			set
			{
				ExtensionDataObject = value;
			}
		}
		#endregion IExtensibleDataObject Implementation
	}
}

